﻿using Microsoft.AspNetCore.Mvc;
using WebHome_API.Models;
using WebHome_API.Models.DBModels;
using WebHome_API.Models.OperResult;

namespace WebHome_API.Controllers
{
    [Route("api/[controller]")]
    [Controller]
    public class TagController : ControllerBase
    {
        [HttpPost("addTag")]
        public OperResult<List<int>> addTag(int id, int type, string tagAddIds, string tagDelIds, int userId = 0)
        {
            using (MyContext content = new())
            {
                bool isTreu = false;
                if (type == (int)FileTypeEnum.Gal && content.Gals.FirstOrDefault(u => u.ID == id) is not null)
                    isTreu = true;
                else if (type == (int)FileTypeEnum.Novel && content.Novels.FirstOrDefault(u => u.ID == id) is not null)
                    isTreu = true;

                if (isTreu)
                {
                    //先写要添加的
                    if (tagAddIds != null)
                    {
                        var tagArr = tagAddIds.Split(',');
                        for (int i = 0; i < tagArr.Length; i++)
                        {
                            var curTag = Convert.ToInt32(tagArr[i]);
                            var tagData = content.FileTags.FirstOrDefault(u => u.ID == curTag && u.TagEnumID == 2);
                            var tagLinkData = content.FileTagLink.FirstOrDefault(u => u.TagID == curTag && u.FileID == id && u.FileType == (FileTypeEnum)type);
                            if (tagData != null && tagLinkData == null)
                            {
                                content.FileTagLink.Add(new FileTagLink_Table()
                                {
                                    TagID = curTag,
                                    FileID = id,
                                    UserID = userId,
                                    FileType = (FileTypeEnum)type,
                                    CreateTime = DateTime.Now,
                                });
                                content.AdminLogs.Add(AdminLog_Table.CreateLog(LogStateEnum.AddGalTag, userId, $"{id},{curTag},{(FileTypeEnum)type}"));
                            }
                        }
                    }
                    //再写要删除的
                    if (tagDelIds != null)
                    {
                        var tagArrDel = tagDelIds.Split(',');
                        for (int i = 0; i < tagArrDel.Length; i++)
                        {
                            var curTag = Convert.ToInt32(tagArrDel[i]);
                            var tagData = content.FileTags.FirstOrDefault(u => u.ID == curTag && u.TagEnumID == 2);
                            var tagLinkData = content.FileTagLink.FirstOrDefault(u => u.TagID == curTag && u.FileID == id && u.FileType == (FileTypeEnum)type);
                            if (tagData != null && tagLinkData != null)
                            {
                                content.FileTagLink.Remove(tagLinkData);
                                content.AdminLogs.Add(AdminLog_Table.CreateLog(LogStateEnum.DelGalTag, userId, $"{id},{curTag},{(FileTypeEnum)type}"));
                            }
                        }
                    }

                    var change = content.SaveChanges();
                    //重新检索
                    List<int> ret = new();
                    content.FileTagLink.Where(u => u.FileID == id && u.FileType == (FileTypeEnum)type).ToList().ForEach(u =>
                    {
                        ret.Add(u.TagID);
                    });
                    return OperResult<List<int>>.Succeed(ret);
                }
                else
                {
                    return OperResult<List<int>>.Failed("当前资源不存在");
                }
            }
        }

        [HttpGet("getTagList")]
        public OperResult<Dictionary<string, List<FileTag_Table>>> getTagList(int type = 0)
        {
            Dictionary<string, List<FileTag_Table>> dict = new();
            using (MyContext context = new())
            {
                if (type == 0)
                {
                    var listOfClub = context.FileTags.Where(u => u.TagEnumID == (int)GalTagEnum.club).ToList();
                    var listOfTrait = context.FileTags.Where(u => u.TagEnumID == (int)GalTagEnum.traits).ToList();
                    dict.Add("club", listOfClub);
                    dict.Add("trait", listOfTrait);
                }
                else
                {
                    var list = context.FileTags.Where(u => u.TagEnumID == type).ToList();
                    dict.Add(((GalTagEnum)type).ToString(), list);
                }
            }
            return OperResult<Dictionary<string, List<FileTag_Table>>>.Succeed(dict);
        }
    }
}
